/*
 * @Author: your name
 * @Date: 2021-07-07 15:01:29
 * @LastEditTime: 2022-04-06 15:41:17
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \exc-tl1-2\SRC\user_src\myQueue.c
 */
#include "myQueue.h"

void queue_Init(QUEUE_T *pQueue, void *pBuffer, BYTE sizeOfElements, BYTE numberOfElements)
{
  pQueue->out = 0;
  pQueue->in = 0;
  pQueue->queueSize = numberOfElements;
  pQueue->count = 0;
  pQueue->elementType = sizeOfElements;
  pQueue->array = (BYTE *)pBuffer;
}

BOOL queue_Enqueue(QUEUE_T *pQueue, void *pElement)
{
  if (pQueue->count >= pQueue->queueSize)
  {
    return FALSE;
  }
  else
  {
    BYTE i;
    BYTE *pEl = pElement;

    for (i = 0; i < pQueue->elementType; i++)
    {
      pQueue->array[(pQueue->in) * (pQueue->elementType) + i] = *(pEl + i);
    }

    pQueue->in = (pQueue->in + 1) % pQueue->queueSize;
    pQueue->count = pQueue->count + 1;
  }
  return TRUE;
}

BOOL queue_Dequeue(QUEUE_T *pQueue, void *pElement)
{
  if (pQueue->count <= 0)
  {
    return FALSE;
  }
  else
  {
    BYTE i;
    BYTE *pEl = pElement;

    for (i = 0; i < pQueue->elementType; i++)
    {
      *(pEl + i) = pQueue->array[(pQueue->out) * (pQueue->elementType) + i];
    }

    pQueue->out = (pQueue->out + 1) % pQueue->queueSize;
    pQueue->count = pQueue->count - 1;
  }
  return TRUE;
}

uint8_t queue_GetItemCount(QUEUE_T *pQueue)
{
  return pQueue->count;
}
